{% extends 'ParentTemplate.html' %}
{% load static %}
{% block content %}

    <div class="container">
         <div class="col-lg-9">
            <hr>

{#        需要做的是先把这个大体的框架搭起来，然后再进行美观一点的设计#}
{#            目前对于页面化的设计是存在问题的，先去做，然后进一步的完善吧#}
            <div class="col-lg-5">
                <div class="panel panel-default">
                    <div class="panel-heading">-作业提交-</div>
                    <div class="panel-body">
                        <div id="m3" style="width: 90%;height: 360px"></div>
                    </div>
{#文件的批量上传#}
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                批量布置作业
            </div>
{#                    的确这里将将文件进行了上传，但是读取文件的过程中有该怎样的显示呢？#}
            <div class="panel-body">
                <form method="post" enctype="multipart/form-data" action="/Inform/multi/">
                     {% csrf_token %}
                    <div class="form-group" >
                        <input type="file" name="exc">
                    </div>
                    <input type="submit" value="上传" class="btn btn-info btn-sm">
                </form>
            </div>

                </div>
            </div>

            <div class="col-lg-6">
                <div class="panel panel-default">
                    <div class="panel-heading" style="background: #b92c28">
                        <h3 class="panel-title" >学生提交作业的排名</h3>
                    </div>
                    <ul class="list-group">
                        <li class="list-group-item" style="background: #2aabd2">1.张三(2023-7-12)</li>
                        <li class="list-group-item" style="background: #67b168">2.李四(2023-7-12)</li>
                        <li class="list-group-item" style="background: #3c763d">3.王五(2023-7-12)</li>
                        <li class="list-group-item" style="background: #985f0d">4.马六(2023-7-12)</li>
                        <li class="list-group-item" style="background: #a6e1ec">5.赵七(2023-7-12)</li>
                    </ul>

                </div>
            </div>
        </div>
        <div class="col-lg-3">
            <div class="panel panel-default">
                <!-- Default panel contents -->
                <div class="panel-heading" style="background:#9f0000">
                    <div style="color: white">
                        作业通知
                        <input  id="btnAdd" style="border-radius: 3px; font-size: 12px; margin-left: 100px " class="btn-success" value="新 增" type="button">
                    </div>
                </div>
                <ul class="list-group">
                   {% for obj in queryset %}
                        <li class="list-group-item list-group-item-info">{{ obj.inform }}
                            <div></div>
                            》
                            <span class="time">{{ obj.create_time }}</span>
{#                            <a style="margin: 10px" class="btn btn-danger btn-xs" href="/Class/delete/?nid={{ mess.id }}">删 除</a>#}
                            <input uid="{{ obj.id }}" style="border-radius: 3px; font-size: 12px; " class="btn-danger btn-delete" value="删 除" type="button">
                            <input uid="{{ obj.id }}" style="border-radius: 3px; font-size: 12px; " class="btn-primary  btn-edit" value="编 辑" type="button">
                        </li>
                    {% endfor %}
                </ul>
            </div>
        </div>
    </div>

     {#    新建订单的对话框#}
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel"></h4>
                </div>
                <div class="modal-body">
                    <form id="formAdd">
                        <div class="clearfix">
                            {% for field in form %}
                                <div class="col-lg-6">
                                    <div class="form-group" style="position: relative;">
                                        <label>{{ field.label }}</label>
                                        {{ field }}
                                        <span class="errors_msg" style="color: #b92c28;position: absolute"></span>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                    <button id="btnSave" type="button" class="btn btn-primary">保 存</button>
                </div>
            </div>
        </div>
    </div>

    {#删除对话框#}
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                    <h4>提示信息</h4>
                    <p style="margin-top: 12px">点击确认后将会删除所有的信息，是否确认？</p>
                    <p style="text-align: right">
                        <button id="btnDeleteConfirm" type="button" class="btn btn-danger">确 认</button>
                        <button type="button" class="btn btn-default " data-dismiss="modal">取 消</button>
                    </p>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block js %}
       <script src="{% static '/js/echarts.min.js' %}"></script>
    <script type="text/javascript">
        var DELETE_ID;
        var EDIT_ID;
        $(function () {
            initPie();
            bindBtnAddEvent();
            bindBtnSaveEvent();
            bindBtnDeleteEvent();
            bindBtnDeleteConfirmEvent();
            bindBtnEditEvent();
        })

        function bindBtnAddEvent() {
            $("#btnAdd").click(function () {
                EDIT_ID = undefined;
                $("#formAdd")[0].reset();
                $(".modal-title").text("新建作业");
                $('#myModal').modal('show');
            });
        }

        //进行数据的添加
        function bindBtnSaveEvent() {
            $("#btnSave").click(function () {
                $(".errors_msg").empty();
                if (EDIT_ID) {
                    //编辑
                    doEdit();
                } else {
                    doAdd();
                }

            });
        }

        function doAdd() {
            $.ajax({
                url: "/Inform/add/",
                type: "post",
                data: $("#formAdd").serialize(),
                dataType: "JSON",
                success: function (res) {
                    {#console.log(res);#}
                    if (res.status) {
                        alert("创建成功");
                        location.reload();
                        $("#formAdd")[0].reset();
                        $('#myModal').modal('hide');

                    }
                }
            })
        }

        function doEdit() {
            $.ajax({
                url: "/Inform/edit/" + "?uid=" + EDIT_ID,
                type: "post",
                data: $("#formAdd").serialize(),
                dataType: "JSON",
                success: function (res) {
                    {#console.log(res);#}
                    if (res.status) {
                        alert("创建成功");

                        $("#formAdd")[0].reset();
                        $('#myModal').modal('hide');
                        location.reload();


                    } else {
                        if (res.tips) {
                            alert(res.tips);
                        } else {
                            $.each(res.error, function (name, errorList) {
                                $("#id_" + name).next().text(errorList[0]);
                            })
                        }

                    }
                }
            })
        }

        //数据的删除
        function bindBtnDeleteEvent() {
            $(".btn-delete").click(function () {
                {#alert("点击了删除")#}
                $("#deleteModal").modal('show');
                DELETE_ID = $(this).attr("uid");
                {#console.log(DELETE_ID)#}

            });
        }

        function bindBtnDeleteConfirmEvent() {
            $("#btnDeleteConfirm").click(function () {
                $.ajax({
                    url: "/Inform/delete/",
                    type: "GET",
                    data: {
                        uid: DELETE_ID
                    },
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            //说明到这里了，是成功的
                            location.reload();
                            {#console.log(DELETE_ID)#}

                        } else {
                            //失败就展示失败的信息
                            alert(res);
                        }
                    },
                })
            })
        }

        //对数据页面的编辑
        function bindBtnEditEvent() {
            $(".btn-edit").click(function () {
                $("#formAdd")[0].reset();
                var uid = $(this).attr("uid");
                EDIT_ID = uid;
                $.ajax({
                    url: "/Inform/detail/",
                    type: "GET",
                    data: {
                        uid: uid
                    },
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            console.log(res);
                            $(".modal-title").text("编辑");
                            $('#myModal').modal('show');
                            $.each(res.data, function (name, value) {
                                //使用数据的拼接,找到数据项,其实上边的console.log(res);来显示数据
                                $("#id_" + name).val(value);
                            });

                        } else {
                            alert(res)
                        }
                    }
                })
            })

        }

                //饼图的制作
          function initPie() {
            var myChart = echarts.init(document.getElementById('m3'));
            var option = {
                title: {
                    text: '提交作业详情',
                    subtext: '计科2102',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    bottom: 0
                },
                series: [
                    {
                        name: '',
                        type: 'pie',
                        radius: '50%',
                        data: [],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            $.ajax({
                url: "/Inform/pie/",
                type: "get",
                dataType: "JSON",
                success: function (res) {
                    //返回后台数据，更新到option中
                    if (res.status) {
                        option.series[0].data = res.data;
                        myChart.setOption(option);
                    }
                }
            })
        }

    </script>

{% endblock %}